#!/bin/bash

E_MISSING_VIRTUAL_HOST_NAME=1
E_DOCUMENT_ROOT_NOT_FOUND=2

hosts=/etc/hosts

function create_virtual_host(){
  echo cool
}

declare -a myArgs

key=0

for i
do
	myArgs[key]="$i"
	key=$(( $key + 1 ))
done

if [ "$1" = 'create' ]  # ex: apacheadmin create virtual host with name my.virtual.host.com
then
  if [ "$2" = 'virtual' -a "$3" = 'host' ]
  then
    name=
    ip=127.0.0.1
    port=80
    
    k=0
    
    for i
    do
      if [ "$i" = 'with' ]
      then
        n=$(( $k + 1 ))
        
        case "${myArgs[$n]}" in
          
          (name)
            n=$(( $n + 1 ))
            
            name="${myArgs[$n]}"
          ;;
          
          (document)
            n=$(( $n + 1 ))

            if [ "${myArgs[$n]}" = 'root' ]
            then
              n=$(( $n + 1 ))
            
              if [ ! -d "${myArgs[$n]}" ]
              then
                echo " Document root pointing to an unexisting folder"
                exit $E_DOCUMENT_ROOT_NOT_FOUND
              fi
            
              documentRoot="${myArgs[$n]}"
            
            fi
          
          ;;
          
          (ip)
            n=$(( $n + 1 ))
            
            ip="${myArgs[$n]}"
          ;;
          
        esac
      fi
      
      k=$(( $k + 1 ))
    done
    
    if [ ! "$name" ]
    then
      echo 'Missing virtual host name ( ` with name VIRTUAL_HOST_NAME`)'
      exit $E_MISSING_VIRTUAL_HOST_NAME
    fi
    
    declare -a lines
    
    ipfound=n
    
    while read line
    do 
      if [[ "$line" == "$ip"* ]]
      then
        ipfound=y
        line="$line $name"
      fi
      
      lines=("${lines[@]}" "$line")
    done < $hosts
    
    if [ $ipfound = 'n' ]
    then
      lines=("${lines[@]}" "$ip $name")
    fi
    
    cp $hosts $hosts.bkp
    
    echo '' > $hosts
    
    for i in "${lines[@]}"
    do
      echo "$i" >> $hosts
    done
    
    vh="<VirtualHost $ip:$port>\n
\t  ServerName $name
"
        
    if [ "$documentRoot" ]
    then
      vh="$vh
\n
\n\t   DocumentRoot $documentRoot
\n   
\n\t   <Directory $documentRoot>
\n\t\t		Options Indexes FollowSymLinks
\n\t\t		AllowOverride None
\n\t\t		Order allow,deny
\n\t\t		allow from all
\n\t	</Directory>"
    fi

    vh="$vh
\n\t  ErrorLog \${APACHE_LOG_DIR}/error.log
\n
\n\t	# Possible values include: debug, info, notice, warn, error, crit,
\n\t	# alert, emerg.
\n\t	LogLevel warn
\n
\n\t	CustomLog \${APACHE_LOG_DIR}/access.log combined
\n</VirtualHost>" 

    echo -e $vh > /etc/apache2/sites-available/apacheadmin-$name
  
    a2ensite apacheadmin-$name

    /etc/init.d/apache2 restart
  fi
fi


#
##  delete virtual host
#

if [ "$1" = 'delete' ]
then
  if [ "$2" = 'virtual' -a "$3" = 'host' ]
  then
    name=
    
    k=0
    
    for i
    do
      if [ "$i" = 'with' ]
      then
        n=$(( $k + 1 ))
        
        case "${myArgs[$n]}" in
          
          (name)
            n=$(( $n + 1 ))
            
            name="${myArgs[$n]}"
          ;;
          
        esac
      fi
      
      k=$(( $k + 1 ))
    done
    
    if [ ! "$name" ]
    then
      echo 'Missing virtual host name ( ` with name VIRTUAL_HOST_NAME`)'
      exit $E_MISSING_VIRTUAL_HOST_NAME
    fi
    
    sed -i 's/'$name'//g' $hosts
    
    a2dissite apacheadmin-$name
    
    rm /etc/apache2/sites-available/apacheadmin-$name

    /etc/init.d/apache2 restart
  fi
fi
